package com.useeinfo.oa.modules.base.dao;

import com.useeinfo.framework.extend.dao.CrudDao;
import com.useeinfo.framework.sugar.data.QueryParam;
import com.useeinfo.framework.sugar.data.QueryUtils;
import com.useeinfo.framework.sugar.data.SelectVO;
import com.useeinfo.framework.sugar.tools.DateTimeUtils;
import com.useeinfo.framework.sugar.tools.StringConverters;
import com.useeinfo.oa.modules.base.entity.Personnel;
import com.useeinfo.oa.modules.base.entity.Position;
import net.sf.json.JSONArray;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;
import java.util.*;

/**
 * @Date: 2018/12/3 17:05
 * @Description: 应用Dao
 */
@Repository("personnelDao")
public class PersonnelDao implements CrudDao<Personnel> {

    private final static Logger logger = LoggerFactory.getLogger(PersonnelDao.class);

    @PersistenceContext
    private EntityManager entityManager;


    @Override
    public Map<String, Object> getSearchCondition(Map<String, String> queryHash) {
        Map<String, Object> conditionHash = new HashMap<>();
        conditionHash.put("s.deleteStatus = ?{paramIndex} ", false);
        if (queryHash == null || queryHash.size() == 0) {
            return conditionHash;
        }
        for(String key:queryHash.keySet()){
            String value = queryHash.get(key);
            switch (key){
                case "mobile":
                    conditionHash.put(" s.mobile= ?{paramIndex}", value);
                    continue;
                case "enterpriseId":
                    conditionHash.put(" exists( select obj from s.enterpriseSet obj where obj.id = ?{paramIndex})", StringConverters.null2Long(value));
                    continue;
                case "enterpriseIds":
                    conditionHash.put(" exists( select obj from s.enterpriseSet obj where obj.id in ("+value+") and 1 = ?{paramIndex})", 1);
                    continue;
                case "organizationId":
                    conditionHash.put(" exists( select obj from s.personnnelOrganizationSet obj where obj.organization.id = ?{paramIndex})", StringConverters.null2Long(value));
                    continue;
                case "likeOrganizationId":
                    conditionHash.put(" exists( select obj from s.personnnelOrganizationSet obj where obj.organization.parentIds like ?{paramIndex})", "%,"+value+",%");
                    continue;
                case "name":
                    conditionHash.put(" s.name like ?{paramIndex}", "%"+value+"%");
                    continue;
                case "beforeCreateDate":
                    conditionHash.put(" s.createDate > ?{paramIndex}", DateTimeUtils.parse("yyyy-MM-dd HH:mm:ss",value));
                    continue;
            }
        }

        return conditionHash;
    }

    @Override
    public Long totalRecord(Map<String, String> queryHash) {

        Map<String, Object> conditions = getSearchCondition(queryHash);
        TypedQuery<Long> typedQuery = QueryUtils.getTypedQueryByCondition("select count(s) from Personnel s ", conditions, entityManager, Long.class);
        return typedQuery.getSingleResult();
    }

    @Override
    public List<Personnel> findList(QueryParam queryParam) {
        String sqlInfo = queryParam.joinJPQL("select s from Personnel s ", "order by s.id desc ");
        Map<String, Object> conditions = getSearchCondition(queryParam.getSqlMap());
        TypedQuery<Personnel> typedQuery = QueryUtils.getTypedQueryByCondition(sqlInfo, conditions, entityManager, Personnel.class);
        return queryParam.findPageList(typedQuery);
    }

    @Override
    public Personnel findModel(Long userId) {
        return entityManager.find(Personnel.class, userId);
    }

    @Override
    public Integer add(Personnel personnel) {
        personnel.setCreateDate(new Date());
        entityManager.persist(personnel);
        logger.info("PersonnelDaoImpl添加personnel成功！");
        return 1;
    }

    @Override
    public Integer update(Personnel personnel) {
        personnel.setUpdateDate(new Date());
        entityManager.merge(personnel);
        return 1;
    }

    @Override
    public Integer delete(Long personnelId) {
        Personnel personnel = entityManager.find(Personnel.class, personnelId);
        entityManager.remove(personnel);
        return 1;
    }

    @Override
    public Integer deleteLogic(Personnel model) {
        model.setDeleteStatus(true);
        return 1;
    }


}
